home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 06 / cpumeter / cpumeter.c < prev    next >
C/C++ Source or Header  |  1990-11-01  |  5KB  |  185 lines

  1. //
  2. // cpumeter.c - (c) 1990 Chiverton Graphics, Inc.
  3. //
  4.  
  5. #define INCL_WIN
  6. #define INCL_DOS
  7. #include <os2.h>
  8. #include <process.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "cpumeter.h"
  12. #include "histgram.h"
  13.  
  14. #define THREADSTACKSIZE   4096
  15.  
  16. VOID TimerThread (void) ;
  17. VOID CounterThread (void) ;
  18. VOID CalibrationThread (void) ;
  19. INT  nearest_10_percent (LONG lAmount);
  20. INT  nearest_1_percent  (LONG lAmount);
  21.  
  22. TID   tidTimer ,
  23.       tidCounter,
  24.       tidCalibration;
  25.  
  26. HWND  hwndClient,
  27.       hwndFrame ;
  28.  
  29. HAB   hab ;
  30.  
  31. HSYSSEM hSem ;
  32.  
  33. LONG  lCount     = 0L,
  34.       lCountMax  = 0L,
  35.       lTenth     = 0L,
  36.       lTwentieth = 0L;
  37.  
  38. INT   iTimerThreadStack       [THREADSTACKSIZE / 2] ,
  39.       iCalibrationThreadStack [THREADSTACKSIZE / 2] ,
  40.       iCounterThreadStack     [THREADSTACKSIZE / 2] ;
  41.  
  42. HPOINTER hIcon [11];
  43.  
  44. INT main (void)
  45.      {
  46.      static CHAR  szClientClass [] = "CPUMETER";
  47.      static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU  | FCF_SIZEBORDER |
  48.                                  FCF_MINMAX   | FCF_TASKLIST |
  49.                                  FCF_ICON     | FCF_SHELLPOSITION ;
  50.      HMQ  hmq  ;
  51.      QMSG qmsg ;
  52.  
  53.      if ( DosCreateSem(CSEM_PUBLIC, &hSem, "\\sem\\cpumeter.sem") )
  54.           DosExit (EXIT_PROCESS, 0);
  55.  
  56.      hab = WinInitialize (0) ;
  57.      hmq = WinCreateMsgQueue (hab, 0) ;
  58.  
  59.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  60.  
  61.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  62.                                      &flFrameFlags, szClientClass,
  63.                                      NULL, 0L, NULL, ID_ICON00, &hwndClient) ;
  64.  
  65.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 31, 1);
  66.      tidCalibration = _beginthread (CalibrationThread, iCalibrationThreadStack, THREADSTACKSIZE, NULL) ;
  67.      DosSleep (1000L);
  68.      DosSuspendThread (tidCalibration);
  69.      DosSetPrty (PRTYS_THREAD, PRTYC_REGULAR, 0, 1);
  70.  
  71.      lTenth     = lCountMax / 10;
  72.      lTwentieth = lCountMax / 20;
  73.  
  74.      tidTimer   = _beginthread (TimerThread,   iTimerThreadStack,   THREADSTACKSIZE, NULL) ;
  75.      tidCounter = _beginthread (CounterThread, iCounterThreadStack, THREADSTACKSIZE, NULL) ;
  76.  
  77.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  78.           WinDispatchMsg (hab, &qmsg) ;
  79.  
  80.      WinDestroyWindow (hwndFrame) ;
  81.      WinDestroyMsgQueue (hmq) ;
  82.      WinTerminate (hab) ;
  83.      return 0 ;
  84.      }
  85.  
  86. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  87.      {
  88.      INT i;
  89.      switch (msg)
  90.           {
  91.           case WM_CREATE:
  92.                {
  93.                for (i=0; i<=10; i++)  hIcon [i] = WinLoadPointer (HWND_DESKTOP, NULL, (100 + i));
  94.                create_histogram (hab, hwnd) ;
  95.                return 0 ;
  96.                }
  97.  
  98.           case WM_SIZE:
  99.                {
  100.                size_histogram (SHORT1FROMMP (mp2), SHORT2FROMMP (mp2));
  101.                return 0 ;
  102.                }
  103.  
  104.           case WM_PAINT:
  105.                {
  106.                paint_histogram ();
  107.                return 0 ;
  108.                }
  109.  
  110.           case WM_SEM1:
  111.                {
  112.                static char szBuffer [60];
  113.                static INT iOld = -1;
  114.  
  115.                i = nearest_1_percent (LONGFROMMP (mp1));
  116.                sprintf (szBuffer, "%d%% CPU Load", i);
  117.                WinSetWindowText (hwndFrame, szBuffer) ;
  118.                update_histogram (i) ;
  119.  
  120.                i = nearest_10_percent (LONGFROMMP (mp1));
  121.                if (i != iOld)
  122.                     {
  123.                     iOld = i;
  124.                     WinSendMsg (hwndFrame, WM_SETICON, hIcon [i], NULL) ;
  125.                     WinInvalidateRect (hwndFrame, NULL, FALSE);
  126.                     WinUpdateWindow (hwndFrame);
  127.                     }
  128.                return 0 ;
  129.                }
  130.  
  131.           case WM_DESTROY:
  132.                {
  133.                for (i=0; i<=10; i++)  WinDestroyPointer (hIcon [i]);
  134.                destroy_histogram ();
  135.                DosCloseSem (hSem);
  136.                return 0 ;
  137.                }
  138.           }
  139.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  140.      }
  141.  
  142. VOID TimerThread ()
  143.      {
  144.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, tidTimer);
  145.      while (TRUE)
  146.           {
  147.           DosSleep (1000L);
  148.  
  149.           WinPostMsg (hwndClient, WM_SEM1, MPFROMLONG (lCount), NULL) ;
  150.           lCount = 0L;
  151.           }
  152.      }
  153.  
  154. VOID CounterThread ()
  155.      {
  156.      DosSetPrty (PRTYS_THREAD, PRTYC_IDLETIME, 0, tidCounter);
  157.      while (TRUE) lCount++;
  158.      }
  159.  
  160. VOID CalibrationThread ()
  161.      {
  162.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 30, tidCalibration);
  163.      while (TRUE) lCountMax++;
  164.      }
  165.  
  166. INT nearest_10_percent (LONG lAmount)
  167.      {
  168.      ldiv_t struct_ldiv;
  169.  
  170.      if (lAmount == 0) return 10;
  171.      if (lAmount >= lCountMax) return 0;
  172.  
  173.      struct_ldiv = ldiv (lAmount, lTenth);
  174.      if (struct_ldiv.rem >= lTwentieth) struct_ldiv.quot++;
  175.      return (INT) (10 - struct_ldiv.quot);
  176.      }
  177.  
  178. INT nearest_1_percent (LONG lAmount)
  179.      {
  180.      if (lAmount == 0) return 100;
  181.      if (lAmount >= lCountMax) return 0;
  182.  
  183.      return (INT) (100. * ((lCountMax - lAmount)/(double)lCountMax));
  184.      }
  185.